package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.baseInfo.domain.StoresInfo;
import com.ruoyi.baseInfo.service.StoresInfoService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;

/**
 * 公告 信息操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private StoresInfoService storesInfoService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 获取首页通知公告列表
     */
    @GetMapping("/homeNoticeList")
    public TableDataInfo homeNoticeList(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selHomeNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 获取基本信息数据
     */
    @GetMapping("/getAlertList")
    public AjaxResult getAlertList()
    {
        List result = new ArrayList();

        // 获取当前用户ID
        String loginUserId = String.valueOf(SecurityUtils.getUserId());
        // 判断当前用户是否为管理员
        boolean isAdmin =  SecurityUtils.isAdmin(Long.parseLong(loginUserId));
        if (isAdmin) {
            return success(result);
        }
        StoresInfo storesInfo = new StoresInfo();
        // 获取客户信息数据
        Map<String, Object> rs = new HashMap<>();
        List<Map<String , Object>> clientsInfoList = storesInfoService.getStoresList(storesInfo);
        if (clientsInfoList.size() <= 0) {
            rs.put("title", "温馨提示，您的客户信息还没有维护");
            result.add(rs);
        }
        // 获取门店信息数据
        rs = new HashMap<>();
        List<Map<String , Object>> storesInfoList = storesInfoService.getStoresList(storesInfo);
        if (storesInfoList.size() <= 0) {
            rs.put("title", "温馨提示，您的门店信息还没有维护");
            result.add(rs);
        }

        return success(result);
    }

    /**
     * 获取消息提示信息数据
     */
    @GetMapping("/getMessageNotices")
    public AjaxResult getMessageNotices(SysNotice notice)
    {
        List<SysNotice> list = noticeService.selMessageNotices(notice);
        return success(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        notice.setCreateBy(getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        notice.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }
}
